/**
 * Created by ss on 2015/1/15.
 */
(function () {
    //缓存页面上的DOM
    var ui = {
        $bf_act: $('.bf_act'),
        $bf_act_li: $('.bf_act_li'),
        $select_btn_01: $('.select_btn_01'),
        $select_btn_02: $('.select_btn_02'),
        $menu_scroll: $('.menu_scroll'),
        $current_url: window.location.href,
        $gou: $('.gou'),
        v_select_01: '',
        v_select_02: '',
        gou_id: '',
        $search: $('#search'),
        $search_01: $('#search_01'),
        $search_02: $('#search_02'),
        $s_text_01: $('#s_text_01'),
        $s_text_02: $('#s_text_02'),
        $s_text_03: $('#s_text_03'),
        $ck_table: $('#ck_table'),
        cj_arr: '',
        $btn_verify: $('.btn_verify'),
        $hidden_verify: $('#hidden_verify'),
        $btn_delete: $('.btn_delete'),
        $hidden_delete: $('#hidden_delete'),
        $del: $('.del'),
        $radio_del: $('#radio_del'),
        $max_length_input: $('#max_length_input'),
        $max_length_input_01:$('#max_length_input_01'),
        $max_no: $('#max_no'),
        $max_no_01: $('#max_no_01'),
        $upload_input: $('#upload_input'),
        $InputFile: $('#InputFile'),
        $upload_btn: $('.upload_btn'),
        $upload_form: $('#upload_form'),
        $btn_back: $('#btn_back')
    };
    var oPage = {
        init: function () {
            this.nav();
            this.select_btn_01();
            this.select_btn_02();
            this.modal();
            this.table_active();
            this.search_fun();
            this.btn_delete();
            this.radio_delete();
            this.search_text();
            this.text_max();
            this.upload_hidden();
            this.btn_back();
            this.text_max_01();
        },
        //左侧菜单active定位效果
        nav: function () {
            ui.$bf_act.find('a').each(function () {
                var $bf_act_a = $(this).attr('href');
                if (ui.$current_url.indexOf($bf_act_a) != -1) {
                    $(this).parent().attr('class', 'active');
                }
            });
            ui.$bf_act_li.find('ul.dk a').each(function () {
                var $bf_act_li_a = $(this).attr('href');
                if (ui.$current_url.indexOf($bf_act_li_a) != -1) {
                    $(this).parent().attr('class', 'active');
                    $(this).parent().parent().parent().attr('class', 'active');
                    // 三层缩放
                    $(this).parents('.bf_act_li').attr('class', 'active');
                }
            });
        },
        //查看状态
        select_btn_01: function () {
            ui.$select_btn_01.each(function () {
                var $that = $(this);
                var select_btn_child = $(this).next().children();
                select_btn_child.on('click', function () {
                    var li_txt = $(this).text();
                    var span = $that.children().first();
                    span.text(li_txt);
                    ui.v_select_01 = $(this).children().attr('id');
                    // 选择值的id ui.v_select_01
                    //console.log(ui.v_select_01);
                });
            });
        },
        //查看栏目
        select_btn_02: function () {
            ui.$select_btn_02.each(function () {
                var $that = $(this);
                var select_btn_child = $(this).next().children();
                select_btn_child.on('click', function () {
                    var li_txt = $(this).text();
                    var span = $that.children().first();
                    span.text(li_txt);
                    ui.v_select_02 = $(this).children().attr('id');
                    // 选择值的id ui.v_select_02
                    //console.log(ui.v_select_02);
                });
            });
        },
        //批量审核
        modal: function () {
            ui.$btn_verify.on('click', function () {
                //清除缓存里的值
                ui.cj_arr = '';
                ui.$ck_table.find('input:checked').each(function () {
                    ui.cj_arr += $(this).attr('id') + '_';
                });
                //把批量审核的id赋值到隐藏字段
                ui.$hidden_verify.attr('value', function () {
                    return ui.cj_arr;
                });
                //console.log(ui.$hidden_verify.val());
            });
        },
        //批量删除
        btn_delete: function () {
            ui.$btn_delete.on('click', function () {
                //清除缓存里的值
                ui.cj_arr = '';
                ui.$ck_table.find('input:checked').each(function () {
                    ui.cj_arr += $(this).attr('id') + '_';
                });
                //把批量删除的id赋值到隐藏字段
                ui.$hidden_delete.attr('value', function () {
                    return ui.cj_arr;
                });
                //console.log(ui.$hidden_delete.val());
            });
        },
        //单选审核按钮
        table_active: function () {
            ui.$gou.on('click', function () {
                var that_class = $(this).attr('class').trim();
                if (that_class === 'm-sm gou') {
                    $(this).attr('data-toggle', '');
                } else {
                    ui.gou_id = $(this).attr('id');
                    //提取叉叉的id
                    //console.log(ui.gou_id);
                    $.ajax({
                        type: "get",
                        url: oPageConfig.oUrl,
                        data: ui.gou_id
                    });
                }
            });
        },
        //单选删除按钮
        radio_delete: function () {
            ui.$del.on('click', function () {
                var that = $(this);
                ui.$radio_del.attr('value', function () {
                    return that.attr('data-id');
                });
                //console.log(ui.$radio_del.val());
            });
        },
        //搜索赋值
        search_fun: function () {
            ui.$search.on('click', function () {
                //搜索内容
                ui.$s_text_01.attr('value', function () {
                    return $(this).val();
                });
                //开始时间
                ui.$s_text_02.attr('value', function () {
                    return $(this).val();
                });
                //结束时间
                ui.$s_text_03.attr('value', function () {
                    return $(this).val();
                });
                //选择状体
                ui.$search_01.attr('value', function () {
                    return ui.v_select_01 ? ui.v_select_01 : ui.$search_01.val();
                });
                //选择栏目
                ui.$search_02.attr('value', function () {
                    return ui.v_select_02 ? ui.v_select_02 : ui.$search_02.val();
                });
            });
        },
        // 把隐藏字段的value赋值到选择框里
        search_text: function () {
            var search_01_text = ui.$search_01.val();
            var search_02_text = ui.$search_02.val();
            // 状态
            ui.$search_01.next().next().find('li').each(function () {
                var that = $(this);
                if (search_01_text === that.children().attr('id')) {
                    ui.$select_btn_01.children().first().text(function () {
                        return that.children().text();
                    });
                }
            });
            // 栏目
            ui.$search_02.next().next().find('li').each(function () {
                var that = $(this);
                if (search_02_text === that.children().attr('id')) {
                    ui.$select_btn_02.children().first().text(function () {
                        return that.children().text();
                    });
                }
            });
        },
        text_max: function () {
            ui.$max_length_input.on('focus keydown keyup', function () {
                ui.$max_no.html(this.value.length);
            });
        },
        text_max_01: function () {
            ui.$max_length_input_01.on('focus keydown keyup', function () {
                ui.$max_no_01.html(this.value.length);
            });
        },
        upload_hidden: function () {
            var upload_id = '';
            ui.$upload_btn.on('click', function () {
                upload_id = this.id;
            });
            ui.$upload_form.on('submit', function (e) {
                ui.$upload_input.val(function () {
                    return upload_id;
                });
                e.stopPropagation();
            });
        },
        btn_back: function () {
            ui.$btn_back.on('click', function () {
                return history.go(-1);
            });
        }
    };
    oPage.init();
})(jQuery);